﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace StudyEnglish.Application.Filter
{
    /// <summary>
    /// Filter for advanced and intuitive filtering any content
    /// </summary>
    public class SmartFilter<T> :BaseFilter
    {

        /// <summary>
        /// List of categorym that belongs to the smart filter
        /// </summary>
        private readonly IList<FilterCategory<T>> _categories = new List<FilterCategory<T>>();

        public override IList<BaseFilterCategory> Categories
        {
            get { return _categories.Select(x => (BaseFilterCategory) x).ToList(); }
        }


        public void AddCategory(FilterCategory<T> category)
        {
            category.Key = NumberShift + _categories.Count;
            _categories.Add(category);
        }


        // ---------------------------------------------------------------------------------------------------
        public Expression<Func<T, bool>> GetFilterWhereCondition()
        {
            var mainPredicate = PredicateBuilder.True<T>();

            return _categories.Aggregate(mainPredicate, (current, category) => current.And(category.GetFilterWhereCondition()));
        }
        
    }
}
